package com.rosesoft.module.mail;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import sun.tools.jar.resources.jar;

/**
 * @FileName HelpUtils.java
 * @Description: 
 * @author rose
 * @version 1.0
 * <p>Company: ROSE-SOFT</p>
 * <p>Copyright: Copyright (c) 2011</p>
 */
public class HelpUtils {
	
	public static void main(String[] args) {
		List allList = new HelpUtils().getEmailAddrs();
		System.out.println(allList.size());
		List finishList = new HelpUtils().getHasFinishEmailAddrs();
		System.out.println(finishList.size());
		String allStr = "";
		String finishStr = "";
		for (Iterator iterator = allList.iterator(); iterator.hasNext();) {
			allStr = iterator.next().toString();
			for (Iterator iterator2 = finishList.iterator(); iterator2.hasNext();) {
				finishStr = iterator2.next().toString();
				if (allStr.trim().equals(finishStr.trim())) {
					iterator.remove();
					break;
				}
			}
		}
		for (int i = 0; i < allList.size(); i++) {
			new HelpUtils().writeFailToDisk(allList.get(i).toString()+"\n");
		}
	}
	
	public List getEmailAddrs(){
		List list = new ArrayList();
		InputStream is = null;
		InputStreamReader isr = null;
		LineNumberReader lnr = null;
		is = this.getClass().getResourceAsStream("maillist.txt");
		isr = new InputStreamReader(is);
		lnr = new LineNumberReader(isr);
		String temp = "";
		try {
			while((temp = lnr.readLine()) != null){
				if (!temp.trim().equals("")) {
					list.add(temp);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				lnr.close();
				isr.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	public List getHasFinishEmailAddrs(){
		List list = new ArrayList();
		InputStream is = null;
		InputStreamReader isr = null;
		LineNumberReader lnr = null;
		is = this.getClass().getResourceAsStream("success_mail.txt");
		isr = new InputStreamReader(is);
		lnr = new LineNumberReader(isr);
		String temp = "";
		try {
			while((temp = lnr.readLine()) != null){
				if (!temp.trim().equals("")) {
					list.add(temp);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				lnr.close();
				isr.close();
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public synchronized void writeFailToDisk(String str){
		OutputStream os = null;
		OutputStreamWriter osw = null;
		try {
			os = new FileOutputStream("F:\\email_info\\email_lost.txt",true);
			osw = new OutputStreamWriter(os);
			try {
				osw.write(str);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				osw.close();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
